package docs;

// import com.google.gdata.client.query;
// import com.google.gdata.client.docs.docsservice;
// import sample.util.simplecommandlineparser;
// import com.google.gdata.data.plaintextconstruct;
// import com.google.gdata.data.docs.documententry;
// import com.google.gdata.data.docs.documentlistentry;
// import com.google.gdata.data.docs.documentlistfeed;
// import com.google.gdata.util.authenticationexception;
// import com.google.gdata.util.serviceexception;

// import java.io.bufferedreader;
// import java.io.file;
// import java.io.ioexception;
// import java.io.inputstreamreader;
// import java.io.printstream;
// import java.net.malformedurlexception;
// import java.net.url;
// import java.util.logging.consolehandler;
// import java.util.logging.level;
// import java.util.logging.logger;

/**
 * an application that serves as a sample to show how the documents list
 * service can be used to search your documents and upload files.
 *  
 * 
 */
public class Docs {

//     /** the message for displaying the usage parameters. */
//     private static final String[] usage_message = {
//         "usage: java Docs.jar --username <user> --password <pass>",
//         "    [--server <host:port>]  where is the feed (default = docs.google.com)",
//         "    [--log]                 enable logging of requests",
//         ""
//     };
// 
//     /** welcome message, introducing the program. */
//     private static final String[] welcome_message = {
//         "this is a demo of the document list feed!", "",
//         "using this interface, you can read and upload your documents.",
//         ""};
// 
//     /** help on all available commands. */
//     private static final String[] command_help_message = {
//         "commands:",
//         " upload full/file/path         "
//             + "[[write the filename of a file to upload]]",
//         " list                          [[shows all documents]]",
//         " spreadsheets                  [[shows all of your spreadsheets]]",
//         " documents                     " 
//             + "[[shows all of your word processing documents]]",
//         " search <search_text>          [[full text query]]",
//         " exit                          [[quit sample]]"};
// 
//     private static final String def_feed_host = "docs.google.com";
//     private static final String feed_url_path = "/feeds/documents/private/full";
//     private static final String document_category = "/-/document";
//     private static final String spreadsheet_category = "/-/spreadsheet";
// 
//     /** our view of doclist service as an authenticated google user. */
//     private docsservice service;
// 
//     /** the url of the doclist feed. */
//     private url documentlistfeedurl;
// 
//     /** the output stream. */
//     private printstream out;
// 
//     /**
//      * constructs a document list demo from the specified document list service, 
//      * which is used to authenticate to and access google documents.
//      *
//      * @param service the connection to the google documents service.
//      * @param outputstream a handle for stdout.
//      * @throws malformedurlexception if the url for the docs feed is invalid.
//      */
//     public Docs(docsservice service, printstream outputstream) 
//         throws malformedurlexception {
//         this.service = service;
//         this.out = outputstream;
//     }
// 
//     /**
//      * log in to google, under the google docs account.
//      *
//      * @param username name of user to authenticate (e.g. yourname@gmail.com)
//      * @param password password to use for authentication
//      * @throws authenticationexception if the service is unable to validate the
//      *         username and password.
//      */
//     public void login(String username, String password)
//         throws authenticationexception {
// 
//         // authenticate
//         service.setusercredentials(username, password);
//     }
// 
//     /**
//      * prints out the specified document entry.
//      *
//      * @param doc the document entry to print
//      */
//     public void printdocumententry(documentlistentry doc) {
//         String shortid = doc.getid().subString(doc.getid().lastindexof('/') + 1);
//         out.println(
//                 " -- document(" + doc.gettitle().getplaintext() + "/" + shortid + ")");
//     }
// 
// 
//     /**
//      * shows all documents that are in the documents list.
//      *
//      * @throws serviceexception when the request causes an error in the google
//      *         docs service.
//      * @throws ioexception when an error occurs in communication with the google
//      *         docs service.
//      */
//     public void showalldocs() throws ioexception, serviceexception {
//         documentlistfeed feed = service.getfeed(documentlistfeedurl, 
//                 documentlistfeed.class);
// 
//         out.println("list of all documents:");
//         for (documentlistentry entry : feed.getentries()) {
//             printdocumententry(entry);
//         }
//     }
// 
//     /**
//      * shows all word processing documents that are in the documents list.
//      *
//      * @throws serviceexception when the request causes an error in the google
//      *         docs service.
//      * @throws ioexception when an error occurs in communication with the google
//      *         docs service.
//      */
//     public void showalldocuments() throws ioexception, serviceexception {
//         documentlistfeed feed = service.getfeed(
//                 new url(documentlistfeedurl.toString() + document_category), 
//                 documentlistfeed.class);
// 
//         out.println("list of all word documents:");
//         for (documentlistentry entry : feed.getentries()) {
//             printdocumententry(entry);
//         }
//     }
// 
//     /**
//      * shows all wor processing documents that are in the documents list.
//      *
//      * @throws serviceexception when the request causes an error in the google
//      *         docs service.
//      * @throws ioexception when an error occurs in communication with the google
//      *         docs service.
//      */
//     public void showallspreadsheets() throws ioexception, serviceexception {
//         documentlistfeed feed = service.getfeed(
//                 new url(documentlistfeedurl.toString() + spreadsheet_category), 
//                 documentlistfeed.class);
// 
//         out.println("list of all spreadsheets:");
//         for (documentlistentry entry : feed.getentries()) {
//             printdocumententry(entry);
//         }
//     }
// 
//     /**
//      * performs a full-text search on your documents.
//      *
//      * @param fulltextsearchString a full text search String, with space-separated
//      *        keywords
//      * @throws serviceexception when the request causes an error in the doclist
//      *         service.
//      * @throws ioexception when an error occurs in communication with the doclis
//      *         service.
//      */
//     public void search(String fulltextsearchString)
//         throws ioexception, serviceexception {
//         query query = new query(documentlistfeedurl);
//         query.setfulltextquery(fulltextsearchString);
//         documentlistfeed feed = service.query(query, documentlistfeed.class);
// 
//         out.println("results for [" + fulltextsearchString + "]");
// 
//         for (documentlistentry entry : feed.getentries()) {
//             printdocumententry(entry);
//         }
//     }
// 
//     /**
//      * performs a full-text search on your documents.
//      *
//      * @param filepath path to uploaded file. 
//      * 
//      * @throws serviceexception when the request causes an error in the doclist
//      *         service.
//      * @throws ioexception when an error occurs in communication with the doclist
//      *         service.
//      */
//     public void uploadfile(String filepath)
//         throws ioexception, serviceexception {
//         documententry newdocument = new documententry();
//         file documentfile = new file(filepath);
//         newdocument.setfile(documentfile);
// 
//         // set the title for the new document. for this example we just use the
//         // filename of the uploaded file.
//         newdocument.settitle(new plaintextconstruct(documentfile.getname()));
//         documentlistentry uploaded =
//             service.insert(documentlistfeedurl, newdocument);
//         printdocumententry(uploaded);
//     }
// 
//     /**
//      * reads and executes one command.
//      *
//      * @param reader to read input from the keyboard
//      * @return false if the user quits, true on exception
//      */
//     public boolean executecommand(bufferedreader reader) {
//         for (String s : command_help_message) {
//             out.println(s);
//         }
// 
//         system.err.print("command: ");
// 
//         try {
//             String command = reader.readline();
//             String[] parts = command.trim().split(" ", 2);
//             String name = parts[0];
//             String parameters = parts.length > 1 ? parts[1] : "";
// 
//             if (name.equals("list")) {
//                 showalldocs();
//             } else if (name.equals("documents")) {
//                 showalldocuments();
//             } else if (name.equals("spreadsheets")) {
//                 showallspreadsheets();
//             } else if (name.equals("search")) {
//                 search(parameters);
//             } else if (name.equals("upload")) {
//                 uploadfile(parameters);
//             } else if (name.startswith("q") || name.startswith("exit")) {
//                 return false;
//             } else {
//                 out.println("unknown command.");
//             }
//         } catch (exception e) {
//             // show *exactly* what went wrong.
//             e.printstacktrace();
//         }
//         return true;
//     }
// 
//     /**
//      * starts up the demo and prompts for commands.
//      *
//      * @param username name of user to authenticate (e.g. yourname@gmail.com)
//      * @param password password to use for authentication
//      * @param feedurl url of the feed to connect to
//      * @throws authenticationexception if the service is unable to validate the
//      *         username and password.
//      */
//     public void run(String username, String password, url feedurl)
//         throws authenticationexception {
//         for (String s : welcome_message) {
//             out.println(s);
//         }
// 
//         documentlistfeedurl = feedurl;
// 
//         bufferedreader reader =
//             new bufferedreader(new inputstreamreader(system.in));
// 
//         // login and prompt the user to enter a command
//         login(username, password);
// 
//         while (executecommand(reader)) {
//         }
//     }


    /**
     * runs the demo.
     *
     * @param args the command-line arguments
     * @throws authenticationexception if the service is unable to validate the
     *         username and password.
     * @throws malformedurlexception if the url for the docs feed is invalid.
     */
//     public static void main(String[] args)
//         throws authenticationexception, malformedurlexception {
//         simplecommandlineparser parser = new simplecommandlineparser(args);
//         String username = parser.getvalue("username", "user", "u");
//         String password = parser.getvalue("password", "pass", "p");
//         String server = parser.getvalue("server", "s");
//         boolean help = parser.containskey("help", "h");
// 
//         if (help || username == null || password == null) {
//             usage();
//             system.exit(1);
//         }
// 
//         Docs demo = new Docs(
//                 new docsservice("document list demo"), system.out);
//         if (server == null) {
//             server = def_feed_host;
//         }
// 
//         if (parser.containskey("log", "l")) {
//             turnonlogging();
//         }
// 
//         demo.run(username, password, new url("http://" + server + feed_url_path));
//     }

//     private static void turnonlogging() {
// 
//         // configure the logging mechanisms
//         logger httplogger =
//             logger.getlogger("com.google.gdata.client.http.httpgdatarequest");
//         httplogger.setlevel(level.all);
//         logger xmllogger = logger.getlogger("com.google.gdata.util.xmlparser");
//         xmllogger.setlevel(level.all);
// 
//         // create a log handler which prints all log events to the console
//         consolehandler loghandler = new consolehandler();
//         loghandler.setlevel(level.all);
//         httplogger.addhandler(loghandler);
//         xmllogger.addhandler(loghandler);
//     }

//     /**
//      * prints out the usage.
//      */
//     private static void usage() {
//         for (String s : usage_message) {
//             system.out.println(s);
//         }
//         for (String s : welcome_message) {
//             system.out.println(s);
//         }
//     }
}
